// pages/empty/empty.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: Number, // 起始页码
    totalPage: Number, //最大页码数
    videlist:'',
    value:'',
  },


  
  onchange(event) {
    console.log(event);
    const value = event.detail
    console.log(value);
    this.setData({
      value: value
    })
  },

  getvidelist(n = '') {
    this.setData({
      EMPLOYEE_ID: app.globalData.mealsList,
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
    })
    wx.showLoading({
      title: '加载中...',
    })

    wx.request({
      url: app.globalData.interface+'/crossVersionOne/API2_2119_CustomerGiftsList',
      method: 'POST',
      data: {
        OPEN_ID: this.data.OPEN_ID, //用户OPEN_ID
        currentPage: "", //当前页   非必填
        session_key: this.data.session_key,
        OPEN_UUID:this.data.jiami,
        EMPLOYEE_ID:this.data.EMPLOYEE_ID,
        KeyWord: this.data.value,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log('res', res);
        console.log('res', res.data.result);
        wx.hideLoading()
        if (res.data.result == 'success' && res.data.data.length != 0) {
          console.log('翻页', res.data.data[0].page.totalPage);

          const copypage = JSON.stringify(res.data.data[0].page.totalPage)
          const totalPage = JSON.parse(copypage)
          console.log('totalPage', totalPage);
          this.setData({
            videlist: res.data.data,
            totalPage: totalPage
          })

          console.log('最大页码数', this.data.totalPage);

        } else {
          wx.showModal({
            title: "提示",
            content: res.data.data,
            showCancel: false,
          });
        }
      },
      fail: (res => {
        wx.hideLoading()
      })
    })


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      EMPLOYEE_ID: app.globalData.mealsList,
      openid: app.globalData.openid,
      jiami: app.globalData.jiami,
      OPEN_ID: app.globalData.openid,
      session_key: app.globalData.session_key,
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


    this.setData({
      page: 1, // 起始页码
    })

    this.getvidelist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let totalP = this.data.totalPage //最大页码数
    const that = this
    let page = that.data.page + 1 // 当前页码+1，获取下一页的数据
    console.log('page', page);

    if (page <= totalP && totalP > 1) {
      that.setData({
        page: page,
      })
      wx.request({
        url: app.globalData.interface+'/crossVersionOne/API2_2119_CustomerGiftsList',
        method: 'POST',
        data: {
          EMPLOYEE_ID:this.data.EMPLOYEE_ID,
          currentPage: page, //当前页   非必填
          OPEN_ID: this.data.openid, //用户OPEN_ID
          session_key: this.data.session_key,
          OPEN_UUID: this.data.jiami,
          KeyWord: this.data.value,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log('res', res);
          console.log('res', res.data.result);

          if (res.data.result == 'success') {
            that.setData({
              videlist: [...that.data.videlist, ...res.data.data], //es6中的在旧数据中加其他数据,
            })

            wx.hideLoading()
            console.log('最大页码数', this.data.totalPage);
            wx.showToast({
              title: '数据请求成功',
              icon: 'success',
              duration: 1000,
            })
          }
        },
        fail: (res => {
          wx.hideLoading()
        })
      })
      console.log("56565656", this.data.page);

    } else {
      wx.showToast({
        title: '没有更多数据了',
        icon: 'error',
        duration: 1500,
      })
    }




  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})